package swingExtensions.dialog;

import io.github.jonestimd.swing.ButtonBarFactory;
import io.github.jonestimd.swing.ComponentFactory;
import io.github.jonestimd.swing.ComponentResources;
import io.github.jonestimd.swing.dialog.ExceptionDialog;
import io.github.jonestimd.swing.dialog.FormDialog;
import net.miginfocom.swing.MigLayout;
import swingExtensions.demo.FrameDemo;

import javax.swing.*;
import java.awt.*;
import java.util.ResourceBundle;

public class FormDialogDemo {

    class SelfFormDialog extends FormDialog {

        public SelfFormDialog(Window owner, String title) {
            super(owner, title, ComponentResources.BUNDLE);
        }

        public void initDialog() {
            JPanel formPanel = getFormPanel();
            formPanel.setLayout(new MigLayout("fill", "[]rel[grow,fill]", ""));
            formPanel.add(new JLabel("名称"));
            formPanel.add(new JTextField(), "wrap");
            formPanel.add(new JLabel("住址"));
            formPanel.add(new JTextField(), "wrap");
            formPanel.add(new JCheckBox("学生"));
        }
    }

    protected JPanel createContentPanel() {
        JButton showForm = new JButton("showForm", null);
        showForm.addActionListener(e -> {
            SelfFormDialog dialog = new SelfFormDialog(null, "Test");
            dialog.initDialog();
            dialog.setVisible(true);
        });
        ButtonBarFactory buttonBarFactory = new ButtonBarFactory();
        buttonBarFactory.add(showForm).border(5, 5);
        return (JPanel) buttonBarFactory.get();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            FormDialogDemo demo = new FormDialogDemo();
            FrameDemo frameDemo = new FrameDemo(FormDialogDemo.class.toString());
            frameDemo.setContentPane(demo.createContentPanel());
            frameDemo.setVisible(true);
        });
    }
}
